import util from '/common/js/util';
import Qs from "qs";

import getDetailData from '/static/json/getbookinfo.json';
var app = getApp();

Page({
  data: {
    book_info: {
      lastzjlist: []
    },
    userNote: {},
    bookId: "",
    scrollTop: 0,
    toView: "",
    str: "qweqwrwr"
  },
  onLoad(e) {

    //console.log("--e:",e);
    this.setData({
      bookId: e.bookId
    });
    this.initFn();

    // dd.enableLeaveConfirm({
    //   effect: ['back', 'close'],
    //   info: {
    //     title: '忘记提交？',
    //     content: '当前页面内容尚未提交，离开将导致内容丢失，确认离开吗？'
    //   },
    //   success: function () {},
    //   fail: function () {},
    //   complete: function () {}
    // })

  },

  initFn() {
    this.detailFn()
  },
  detailFn() {
    let api = "/user/book/detail",
      list = {
        bookId: this.data.bookId
      };
    dd.showLoading({
      content: '数据加载中...',
    })
    this.setData({
      showLoadMoreTxt: "load"
    });
    app.$post(api, list).then(res => {
      dd.hideLoading();

      if (res.code == 200) {
        let book_info = res.data;
        book_info.lastzjlist = [];
        if (util.isNotEmpty(book_info.chapter)) {

          let names = book_info.chapter.split("，");
          // let arr=[];
          // for(let i=0;i<names.length;i++){
          //   arr.push({
          //     index: i,
          //     chapter: i + 1,
          //     name: names[i],
          //     chapterid: i+1
          //   });
          // }
          // console.log("--names:",names);
          book_info.lastzjlist = names;
        }
        this.setData({
          book_info
        })
        this.getNewUserNoteFn();
        //  console.log("--book_info:",book_info);
        dd.stopPullDownRefresh();
      } else {

      }
    }).catch(function (error) {
      console.log("---error");
      console.log(error)
    });
  },
  getNewUserNoteFn() {
    let api = "/user/book/getNewUserNote",
      list = {
        bookId: this.data.bookId
      };
    app.$post(api, list).then(res => {
      if (res.code == 200) {
        let userNote = res.data;

        this.setData({
          userNote
        })

      } else {

      }
    }).catch(function (error) {
      console.log("---error");
      console.log(error)
    });

  },
  readBookFn(){
    let api = "/user/book/readBook",
    list = {
      bookId: this.data.bookId
    };
  app.$post(api, list).then(res => {
    if (res.code == 200) {
      let book_info = res.data;
      if(book_info.fileInfo){
        let json=JSON.parse(book_info.fileInfo);
  
  
        dd.previewFileInDingTalk({
        fileId: json.id,
        spaceId: json.spaceId,
        fileName: json.name,
        fileSize: json.size,
        fileType: json.extension,
        success: (e) => {},
        fail: (err) => {
          console.log("---err::",err);
        },
        complete: (e) => {},
      });
      }else if(book_info.fileUrl){
        this.goWebView(e)
      }
      

    } else {

    }
  }).catch(function (error) {
    console.log("---error");
    console.log(error)
  });
  },

  goWebView(e) {
    let url = e.currentTarget.dataset.url;
    let str = url.replaceAll("https://liuqingfeng-read-buket.oss-cn-shenzhen.aliyuncs.com/file/", "");
    let arr = str.split(".");
    let FILE_TYPE = arr[arr.length-1];
    let data = {
      url,
      FILE_TYPE
    };
    console.log(data);
    let p = Qs.stringify(data);
    let path = '/pages/web_view/file_view/file_view';
    let to_url = `${path}?${p}`;
    dd.navigateTo({
      url: to_url
    })
  },
  goFileView(e){
    this.readBookFn();
    // let book_info=this.data.book_info;
    // if(book_info.fileInfo){
    //   let json=JSON.parse(book_info.fileInfo);


    //   dd.previewFileInDingTalk({
    //   fileId: json.id,
    //   spaceId: json.spaceId,
    //   fileName: json.name,
    //   fileSize: json.size,
    //   fileType: json.extension,
    //   success: (e) => {},
    //   fail: (err) => {
    //     console.log("---err::",err);
    //   },
    //   complete: (e) => {},
    // });
    // }else if(book_info.fileUrl){
    //   this.goWebView(e)
    // }
  },
  previewImageFn(e) {
    let img = e.target.dataset.img;
    if (!img) {
      return;
    }
    dd.previewImage({
      urls: [
        img
      ],
      current: 1,
      success: () => {},
      fail: () => {},
      complete: () => {},
    });
  },
  gobookChapter(e) {

    let data = e.target.dataset;
    data.bookId = this.data.book_info.bookId;
    data.bookName = this.data.book_info.bookName;
    data.userName = app.prototype.$userName;
    data.userId = app.prototype.$userId;

    this.gotoNote(data);
    // index value
  },
  onTapFabClick(e) {
    this.setData({
      scrollTop: 0,
      toView: "top"
    })
  },
  scroll(e) {
    this.setData({
      scrollTop: e.detail.scrollTop,
      toView: ""
    })
  },
  onScrollToLower() {
    console.log("---开始触底");
  },

  gotoNote(query) {
    // let query={};
    // query.bookId=this.data.book_info.bookId;
    // query.bookName=this.data.book_info.bookName;
    // query.userName=app.prototype.$userName;
    // query.userId=app.prototype.$userId;
    //  query.queryDate=;
    let p = Qs.stringify(query);
    let path = '/pages/note/noteDetail/noteDetail';
    console.log("---p:", p);
    let url = `${path}?${p}`;
    dd.navigateTo({
      url: url
    })
  },
  gotobookNote() {
    let query = {};
    query.bookId = this.data.book_info.bookId;
    query.bookName = this.data.book_info.bookName;
    query.userName = app.prototype.$userName;
    query.userId = app.prototype.$userId;
    this.gotoNote(query);
  },
  gotoNoteAll() {
    let query = {};
    query.bookId = this.data.book_info.bookId;
    query.bookName = this.data.book_info.bookName;
    query.userName = app.prototype.$userName;
    query.userId = app.prototype.$userId;
    query.queryType = "all";
    let p = Qs.stringify(query);
    let path = '/pages/note/noteGroup/noteGroup';
    // console.log("---p:",p);
    let url = `${path}?${p}`;
    dd.navigateTo({
      url: url
    })
  },

});